package com.lcj.util;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author lcj
 */
@Slf4j
public class IpUtil {

    private IpUtil() {
    }

    /**
     * 根据ip获取详细地址
     * 临时使用，待调整
     */
    public static String getHttpCityInfo(String ip) {
        String url = String.format("http://whois.pconline.com.cn/ipJson.jsp?ip=%s&json=true", ip);
        try (HttpResponse response = HttpUtil.createGet(url).
                header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)" +
                        " Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0").execute()) {
            if (response.isOk()) {
                String body = response.body();
                JSONObject object = JSONUtil.parseObj(body);
                return object.getStr("addr");
            }
            log.error("获取IP失败:{}", response.body());
        }
        return "unknown";
    }
}
